
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.ForumUser ADD
	ExternalAccount bit NULL
GO
ALTER TABLE dbo.ForumUser SET (LOCK_ESCALATION = TABLE)
GO
COMMIT


UPDATE ForumUser SET ExternalAccount = 0;


/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_ForumUser
	(
	Id int NOT NULL IDENTITY (1, 1),
	ProviderId nvarchar(50) NOT NULL,
	Name nvarchar(256) NOT NULL,
	EmailAddress nvarchar(200) NOT NULL,
	FirstVisit datetime NOT NULL,
	LastVisit datetime NOT NULL,
	LastIP nvarchar(50) NULL,
	Deleted bit NOT NULL,
	Active bit NOT NULL,
	Timezone nvarchar(100) NOT NULL,
	Culture nvarchar(10) NOT NULL,
	FullName nvarchar(200) NULL,
	UserFlag int NOT NULL,
	UseFullName bit NOT NULL,
	ExternalAccount bit NOT NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_ForumUser SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_ForumUser ON
GO
IF EXISTS(SELECT * FROM dbo.ForumUser)
	 EXEC('INSERT INTO dbo.Tmp_ForumUser (Id, ProviderId, Name, EmailAddress, FirstVisit, LastVisit, LastIP, Deleted, Active, Timezone, Culture, FullName, UserFlag, UseFullName, ExternalAccount)
		SELECT Id, ProviderId, Name, EmailAddress, FirstVisit, LastVisit, LastIP, Deleted, Active, Timezone, Culture, FullName, UserFlag, UseFullName, ExternalAccount FROM dbo.ForumUser WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_ForumUser OFF
GO
ALTER TABLE dbo.Forum
	DROP CONSTRAINT [FK_dbo.Forum_dbo.ForumUser_LastPostUserId]
GO
ALTER TABLE dbo.Topic
	DROP CONSTRAINT [FK_dbo.Topic_dbo.ForumUser_LastPostAuthorId]
GO
ALTER TABLE dbo.Topic
	DROP CONSTRAINT [FK_dbo.Topic_dbo.ForumUser_AuthorId]
GO
ALTER TABLE dbo.Post
	DROP CONSTRAINT [FK_dbo.Post_dbo.ForumUser_AuthorId]
GO
ALTER TABLE dbo.Attachment
	DROP CONSTRAINT [FK_dbo.Attachment_dbo.ForumUser_AuthorId]
GO
ALTER TABLE dbo.FollowTopic
	DROP CONSTRAINT [FK_dbo.FollowTopic_dbo.ForumUser_ForumUserId]
GO
ALTER TABLE dbo.FollowForum
	DROP CONSTRAINT [FK_dbo.FollowForum_dbo.ForumUser_ForumUserId]
GO
ALTER TABLE dbo.GroupMember
	DROP CONSTRAINT [FK_dbo.GroupMember_dbo.ForumUser_ForumUserId]
GO
ALTER TABLE dbo.ForumTrack
	DROP CONSTRAINT [FK_dbo.ForumTrack_dbo.ForumUser_ForumUserId]
GO
ALTER TABLE dbo.TopicTrack
	DROP CONSTRAINT [FK_dbo.TopicTrack_dbo.ForumUser_ForumUserId]
GO
ALTER TABLE dbo.PostReport
	DROP CONSTRAINT [FK_dbo.PostReport_dbo.ForumUser_ReportedById]
GO
ALTER TABLE dbo.PostReport
	DROP CONSTRAINT [FK_dbo.PostReport_dbo.ForumUser_ResolvedById]
GO
DROP TABLE dbo.ForumUser
GO
EXECUTE sp_rename N'dbo.Tmp_ForumUser', N'ForumUser', 'OBJECT' 
GO
ALTER TABLE dbo.ForumUser ADD CONSTRAINT
	[PK_dbo.ForumUser] PRIMARY KEY CLUSTERED 
	(
	Id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.PostReport ADD CONSTRAINT
	[FK_dbo.PostReport_dbo.ForumUser_ReportedById] FOREIGN KEY
	(
	ReportedById
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.PostReport ADD CONSTRAINT
	[FK_dbo.PostReport_dbo.ForumUser_ResolvedById] FOREIGN KEY
	(
	ResolvedById
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.PostReport SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TopicTrack ADD CONSTRAINT
	[FK_dbo.TopicTrack_dbo.ForumUser_ForumUserId] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.TopicTrack SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.ForumTrack ADD CONSTRAINT
	[FK_dbo.ForumTrack_dbo.ForumUser_ForumUserId] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.ForumTrack SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GroupMember ADD CONSTRAINT
	[FK_dbo.GroupMember_dbo.ForumUser_ForumUserId] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GroupMember SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FollowForum ADD CONSTRAINT
	[FK_dbo.FollowForum_dbo.ForumUser_ForumUserId] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FollowForum SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FollowTopic ADD CONSTRAINT
	[FK_dbo.FollowTopic_dbo.ForumUser_ForumUserId] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FollowTopic SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Attachment ADD CONSTRAINT
	[FK_dbo.Attachment_dbo.ForumUser_AuthorId] FOREIGN KEY
	(
	AuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Attachment SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Post ADD CONSTRAINT
	[FK_dbo.Post_dbo.ForumUser_AuthorId] FOREIGN KEY
	(
	AuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Post SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Topic ADD CONSTRAINT
	[FK_dbo.Topic_dbo.ForumUser_LastPostAuthorId] FOREIGN KEY
	(
	LastPostAuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Topic ADD CONSTRAINT
	[FK_dbo.Topic_dbo.ForumUser_AuthorId] FOREIGN KEY
	(
	AuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Topic SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Forum ADD CONSTRAINT
	[FK_dbo.Forum_dbo.ForumUser_LastPostUserId] FOREIGN KEY
	(
	LastPostUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Forum SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
